﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.Data;

using WinnerCMS.Common;

using WinnerCMS.Model;
namespace WinnerCMS.Data
{
    public class MessageDal
    {

        #region 发短信
        /// <summary>
        /// 发短信
        /// </summary>
        public void Add(Message model)
        {
            using (Entities db = new Entities())
            {
                db.Messages.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        public void Delete(int[] ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Messages where ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.Messages.Remove(x));
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        public void LogicDeleteOrRestoration(int id)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Messages where x.Id == id select x;
                q.ToList().ForEach(x => x.DelSend = !x.DelSend);

                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 删除指定会员的所有数据
        /// <summary>
        /// 删除指定会员的所有数据
        /// </summary>
        /// <param name="userNames"></param>
        public void DelForUser(string[] userNames)
        {
            using (Entities db = new Entities())
            {
                var q = db.Messages.Where(x => userNames.Contains(x.Sender));
                q.ToList().ForEach(x => db.Messages.Remove(x));

                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 统计短信数量
        /// <summary>
        /// 统计未读短信数量
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int CountNoRead(string userName)
        {
            using (Entities db = new Entities())
            {
                return db.Messages.Count(x => x.Incept == userName && !x.IsRead);
            }
        }
        #endregion

        #region 读短信
        /// <summary>
        /// 读短信
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public Message ReadMsg(string guid)
        {
            using (Entities db = new Entities())
            {
                var q = db.Messages.First(x => x.GUID == guid);
                q.IsRead = true;
                db.SaveChangesAsync().Wait();

                return q;
            }
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pp"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public IList<Message> BindList(string sort, string keyword, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = (from A in db.Messages
                             select A);
                switch (sort)
                {
                    case "Incept":
                        query = query.Where(x => x.Incept == keyword);
                        break;
                    case "Sender":
                        query = query.Where(x => x.Sender == keyword);
                        break;
                    case "DelSend":
                        query = query.Where(x => x.Incept == keyword && x.DelSend);
                        break;
                    case "Title":
                        query = query.Where(x => x.Title.StartsWith(keyword));
                        break;
                    default:
                        throw new NotSupportedException("搜索不支持的字段。");
                }

                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }

        #endregion
    }
}
